package com.ss.android.crash.log;

import android.app.ActivityManager;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Build;
import android.os.Debug;
import android.os.Process;
import android.telephony.TelephonyManager;
import android.text.TextUtils;

import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.Closeable;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.zip.Deflater;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

public final class CrashUtils {
    enum CompressType {
        NONE(0),
        GZIP(1),
        DEFLATER(2);

        final int nativeInt;

        private CompressType(int i) {
            this.nativeInt = i;
        }
    }


    public enum NetworkType {
        NONE(0),
        MOBILE(1),
        MOBILE_2G(2),
        MOBILE_3G(3),
        WIFI(4),
        MOBILE_4G(5);

        final int nativeInt;

        private NetworkType(int i) {
            this.nativeInt = i;
        }

        public final int getValue() {
            return this.nativeInt;
        }
    }

    private static String a;
    private static Set b;
    private static Set c;
    private static boolean d;
    private static boolean e;

    static {
        CrashUtils.a = null;
        CrashUtils.b = new HashSet();
        CrashUtils.c = new HashSet();
        CrashUtils.b.add("ThreadPlus");
        CrashUtils.b.add("ApiDispatcher");
        CrashUtils.b.add("ApiLocalDispatcher");
        CrashUtils.b.add("AsyncLoader");
        CrashUtils.b.add("AsyncTask");
        CrashUtils.b.add("Binder");
        CrashUtils.b.add("PackageProcessor");
        CrashUtils.b.add("SettingsObserver");
        CrashUtils.b.add("WifiManager");
        CrashUtils.b.add("JavaBridge");
        CrashUtils.b.add("Compiler");
        CrashUtils.b.add("Signal Catcher");
        CrashUtils.b.add("GC");
        CrashUtils.b.add("ReferenceQueueDaemon");
        CrashUtils.b.add("FinalizerDaemon");
        CrashUtils.b.add("FinalizerWatchdogDaemon");
        CrashUtils.b.add("CookieSyncManager");
        CrashUtils.b.add("RefQueueWorker");
        CrashUtils.b.add("CleanupReference");
        CrashUtils.b.add("VideoManager");
        CrashUtils.b.add("DBHelper-AsyncOp");
        CrashUtils.b.add("InstalledAppTracker2");
        CrashUtils.b.add("AppData-AsyncOp");
        CrashUtils.b.add("IdleConnectionMonitor");
        CrashUtils.b.add("LogReaper");
        CrashUtils.b.add("ActionReaper");
        CrashUtils.b.add("Okio Watchdog");
        CrashUtils.b.add("CheckWaitingQueue");
        CrashUtils.c.add("com.facebook.imagepipeline.core.PriorityThreadFactory");
        CrashUtils.c.add("com.ss.android.common.util.SimpleThreadFactory");
        CrashUtils.d = false;
        CrashUtils.e = false;
    }

    public static String a(Context arg4) {
        Object v0_2;
        String v0 = CrashUtils.a;
        if (TextUtils.isEmpty(((CharSequence) v0))) {
            try {
                int v1 = Process.myPid();
                Iterator v2 = ((ActivityManager) arg4.getSystemService(Context.ACTIVITY_SERVICE)).getRunningAppProcesses().iterator();
                do {
                    if (!v2.hasNext()) {
                        v0 = CrashUtils.d();
                        CrashUtils.a = v0;
                        return v0;
                    }

                    v0_2 = v2.next();
                }
                while (((ActivityManager.RunningAppProcessInfo) v0_2).pid != v1);

                v0 = ((ActivityManager.RunningAppProcessInfo) v0_2).processName;
                CrashUtils.a = v0;
                return v0;
            } catch (Exception v0_1) {
                v0_1.printStackTrace();
            }

            label_19:
            v0 = CrashUtils.d();
            CrashUtils.a = v0;
        }

        return v0;
    }

    private static int a(ActivityManager arg3) {
        int v0 = -1;
        if (Build.VERSION.SDK_INT >= 11) {
            try {
                v0 = arg3.getLargeMemoryClass();
            } catch (Throwable v1) {
            }
        }

        return v0;
    }

    public static String a() {
        return CrashUtils.b("ro.build.version.emui");
    }

    private static String a(NetworkType arg3) {
        String v0 = "";
        try {
            switch (o.a[arg3.ordinal()]) {
                case 1: {
                    return "wifi";
                }
                case 2: {
                    return "2g";
                }
                case 3: {
                    return "3g";
                }
                case 4: {
                    return "4g";
                }
                case 5: {
                    return "mobile";
                }
            }

            return v0;

        } catch (Exception v1) {
        }

        return v0;
    }

    private static String a(String arg2, String arg3) {
        try {
            return URLEncoder.encode(arg2, arg3);
        } catch (UnsupportedEncodingException v0) {
            throw new IllegalArgumentException(((Throwable) v0));
        }
    }

    static void a(Context arg6, JSONObject arg7) {
        try {
            if (arg6 != null) {
                arg6 = arg6.getApplicationContext();
            }
            Debug.MemoryInfo v0_1 = new Debug.MemoryInfo();
            Debug.getMemoryInfo(v0_1);
            JSONObject v1 = new JSONObject();
            v1.put("dalvikPrivateDirty", v0_1.dalvikPrivateDirty);
            v1.put("dalvikPss", v0_1.dalvikPss);
            v1.put("dalvikSharedDirty", v0_1.dalvikSharedDirty);
            v1.put("nativePrivateDirty", v0_1.nativePrivateDirty);
            v1.put("nativePss", v0_1.nativePss);
            v1.put("nativeSharedDirty", v0_1.nativeSharedDirty);
            v1.put("otherPrivateDirty", v0_1.otherPrivateDirty);
            v1.put("otherPss", v0_1.otherPss);
            v1.put("otherSharedDirty", v0_1.otherSharedDirty);
            v1.put("totalPrivateClean", p.a(v0_1));
            v1.put("totalPrivateDirty", v0_1.getTotalPrivateDirty());
            v1.put("totalPss", v0_1.getTotalPss());
            v1.put("totalSharedClean", p.b(v0_1));
            v1.put("totalSharedDirty", v0_1.getTotalSharedDirty());
            v1.put("totalSwappablePss", p.c(v0_1));
            arg7.put("memory_info", v1);
            Object v0_2 = null;
            if (arg6 != null) {
                v1 = new JSONObject();
                ActivityManager.MemoryInfo v2 = new ActivityManager.MemoryInfo();
                v0_2 = arg6.getSystemService(Context.ACTIVITY_SERVICE);
                ((ActivityManager) v0_2).getMemoryInfo(v2);
                v1.put("availMem", v2.availMem);
                v1.put("lowMemory", v2.lowMemory);
                v1.put("threshold", v2.threshold);
                v1.put("totalMem", q.a(v2));
                arg7.put("sys_memory_info", v1);
            }

            v1 = new JSONObject();
            v1.put("native_heap_size", Debug.getNativeHeapSize());
            v1.put("native_heap_alloc_size", Debug.getNativeHeapAllocatedSize());
            v1.put("native_heap_free_size", Debug.getNativeHeapFreeSize());
            Runtime v2_1 = Runtime.getRuntime();
            v1.put("max_memory", v2_1.maxMemory());
            v1.put("free_memory", v2_1.freeMemory());
            v1.put("total_memory", v2_1.totalMemory());
            if (v0_2 != null) {
                v1.put("memory_class", ((ActivityManager) v0_2).getMemoryClass());
                v1.put("large_memory_class", CrashUtils.a(((ActivityManager) v0_2)));
            }

            arg7.put("app_memory_info", v1);
        } catch (Throwable v0) {
        }
    }

    public static void a(Closeable arg1) {
        if (arg1 != null) {
            try {
                arg1.close();
            } catch (IOException v0) {
            }
        }
    }

    public static boolean a(String arg3) {
        boolean v0 = true;
        if (TextUtils.isEmpty(((CharSequence) arg3))) {
            arg3 = CrashUtils.b("ro.build.version.emui");
        }

        if (((TextUtils.isEmpty(((CharSequence) arg3))) || !arg3.toLowerCase().startsWith("emotionui")) && !CrashUtils.e()) {
            v0 = false;
        }

        return v0;
    }

    static boolean a(String arg6, Map arg7) {
        String v0_3;
        String v1;
        try {
            if (TextUtils.isEmpty(((CharSequence) arg6))) {
                return false;
            }
            v1 = "http://log.snssdk.com/service/2/app_log_exception/";
            if (!TextUtils.isDigitsOnly(((CharSequence) v1)) && arg7 != null) {
                if (v1.indexOf("?") < 0) {
                    v1 = v1 + "?";
                }

                if (arg7 == null) {

                } else if (arg7.size() <= 0) {

                } else {
                    Iterator v3 = arg7.entrySet().iterator();
                    while (true) {
                        if (!v3.hasNext()) {
                            break;
                        }

                        Object v0_2 = v3.next();
                        if (arg7.get(((Map.Entry) v0_2).getKey()) == null) {
                            v0_3 = v1;
                            v1 = v0_3;
                            continue;
                        } else if (v1.endsWith("?")) {
                            v1 = v1 + CrashUtils.a(((Map.Entry) v0_2).getKey().toString(), "UTF-8") + "=" + CrashUtils.a(arg7.get(((Map.Entry) v0_2).getKey()).toString(), "UTF-8");
                            continue;
                        } else {
                            v0_3 = v1 + "&" + CrashUtils.a(((Map.Entry) v0_2).getKey().toString(), "UTF-8") + "=" + CrashUtils.a(arg7.get(((Map.Entry) v0_2).getKey()).toString(), "UTF-8");
                        }
                        v1 = v0_3;
                        continue;
                    }
                }
            }

            try {
                label_9:
                return CrashUtils.a(v1, arg6.getBytes(), CompressType.GZIP, "application/json; charset=utf-8");
            } catch (Throwable v0) {
                return false;
            }
        } catch (Throwable v0) {
            return false;
        }
    }

    private static boolean a(String arg6, byte[] arg7, CompressType arg8, String arg9) throws Throwable {
        ByteArrayOutputStream v0_2;
        int v5 = 128;
        int v4 = 8192;
        if (arg6 == null) {
            boolean v0 = false;
            return v0;
        }

        if (arg7 == null) {
            arg7 = new byte[0];
        }

        int v2 = arg7.length;
        String v0_1 = null;
        if (CompressType.GZIP == arg8 && v2 > v5) {
            v0_2 = new ByteArrayOutputStream(v4);
            GZIPOutputStream v2_1 = new GZIPOutputStream(((OutputStream) v0_2));
            try {
                v2_1.write(arg7);
            } catch (Throwable v0_3) {
                v2_1.close();
                return false;
            }

            v2_1.close();
            arg7 = v0_2.toByteArray();
            v0_1 = "gzip";
        } else if (CompressType.DEFLATER == arg8 && v2 > v5) {
            v0_2 = new ByteArrayOutputStream(v4);
            Deflater v2_2 = new Deflater();
            v2_2.setInput(arg7);
            v2_2.finish();
            byte[] v3 = new byte[v4];
            while (!v2_2.finished()) {
                v0_2.write(v3, 0, v2_2.deflate(v3));
            }

            v2_2.end();
            arg7 = v0_2.toByteArray();
            v0_1 = "deflate";
        }

        return CrashUtils.a(arg6, arg7, arg9, v0_1, "POST");
    }

    private static boolean a(String arg5, byte[] arg6, String arg7, String arg8, String arg9) throws Throwable {
        InputStream v3_1 = null;
        try {
            URLConnection v0_1 = new URL(arg5).openConnection();
            ((HttpURLConnection) v0_1).setDoOutput(true);
            if (arg7 != null) {
                ((HttpURLConnection) v0_1).setRequestProperty("Content-Type", arg7);
            }

            if (arg8 != null) {
                ((HttpURLConnection) v0_1).setRequestProperty("Content-Encoding", arg8);
            }

            ((HttpURLConnection) v0_1).setRequestProperty("Accept-Encoding", "gzip");
            ((HttpURLConnection) v0_1).setRequestMethod(arg9);
            if (arg6 != null && arg6.length > 0) {
                DataOutputStream v3 = new DataOutputStream(((HttpURLConnection) v0_1).getOutputStream());
                v3.write(arg6);
                v3.flush();
                v3.close();
            }

            if (((HttpURLConnection) v0_1).getResponseCode() != 200) {
                return false;
            }

            v3_1 = ((HttpURLConnection) v0_1).getInputStream();
            if ("gzip".equalsIgnoreCase(((HttpURLConnection) v0_1).getContentEncoding())) {
                GZIPInputStream v0_2 = new GZIPInputStream(v3_1);
                CrashUtils.a(((InputStream) v0_2));
                v0_2.close();
            } else {
                CrashUtils.a(v3_1);
            }
            if (v3_1 != null) {
                v3_1.close();
            }
            return true;
        } catch (Throwable v0) {
            return false;
        } finally {
            if (v3_1 != null) {
                try {
                    v3_1.close();
                } catch (Exception e) {

                }
            }
        }
    }

    private static byte[] a(InputStream arg4) throws IOException {
        ByteArrayOutputStream v0 = new ByteArrayOutputStream();
        byte[] v1 = new byte[8192];
        while (true) {
            int v3 = arg4.read(v1);
            if (-1 == v3) {
                break;
            }

            v0.write(v1, 0, v3);
        }

        if (arg4 != null) {
            arg4.close();
        }

        return v0.toByteArray();
    }

    private static String b(String arg5) {
        Throwable v0_1;
        BufferedReader v1_1 = null;
        String v0 = null;
        try {
            v1_1 = new BufferedReader(new InputStreamReader(Runtime.getRuntime().exec("getprop " + arg5).getInputStream()), 1024);
            v0 = v1_1.readLine();
            return v0;
        } catch (Throwable v2) {
            return v0;
        } finally {
            if (v1_1 != null) {
                try {
                    v1_1.close();
                } catch (IOException v1_2) {
                }
            }
        }
    }

    static String b(Context arg3) throws NullPointerException {
        CharSequence v0 = null;
        if (arg3 == null) {
            throw new NullPointerException("Context is NUll");
        }

        try {
            if (arg3.getCacheDir() != null) {
                v0 = arg3.getCacheDir().getPath();
            } else {
                File v1_1 = arg3.getDir("/data/data/" + arg3.getPackageName() + "/cache/", 0);
                if (v1_1 == null) {
                } else {
                    v0 = v1_1.getPath();
                }
            }
        } catch (Throwable v1) {
        }

        if (TextUtils.isEmpty(v0)) {
            throw new NullPointerException("Cannot Create Cache Dir");
        }
        return ((String) v0);
    }

    public static boolean b() {
        if (!CrashUtils.e) {
            try {
                if (Class.forName("miui.os.Build") == null) {

                } else {
                    CrashUtils.d = true;
                    CrashUtils.e = true;
                    boolean v0_1 = CrashUtils.d;
                    return v0_1;
                }
            } catch (Exception v0) {

            }
            CrashUtils.e = true;
        }

        return CrashUtils.d;
    }

    static String c(Context arg1) {
        return CrashUtils.a(CrashUtils.e(arg1));
    }

    public static boolean c() {
        boolean v0 = Build.DISPLAY.indexOf("Flyme") >= 0 || (Build.USER.equals("flyme")) ? true : false;
        return v0;
    }

    private static String d() {
        Throwable v0_1;
        BufferedReader v1_1 = null;
        String v0 = null;
        try {
            v1_1 = new BufferedReader(new InputStreamReader(new FileInputStream("/proc/" + Process.myPid() + "/cmdline"), "iso-8859-1"));
            StringBuilder v2_1 = new StringBuilder();
            while (true) {
                int v3 = v1_1.read();
                if (v3 <= 0) {
                    break;
                }
                v2_1.append(((char) v3));
            }
            return v2_1.toString();
        } catch (Throwable v2) {
            return null;
        } finally {
            if (v1_1 != null) {
                try {
                    v1_1.close();
                } catch (Exception v1_2) {
                }
            }
        }
    }

    public static boolean d(Context arg2) {
        try {
            NetworkInfo v0_1 = ((ConnectivityManager) arg2.getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();
            if (v0_1 != null) {
                if (!v0_1.isAvailable()) {
                    return false;
                }

                return true;
            }

            return false;
        } catch (Exception v0) {
            boolean v0_2 = false;
            return v0_2;
        }
    }

    private static boolean e() {
        boolean v0 = false;
        try {
            if ((TextUtils.isEmpty(Build.BRAND)) || !Build.BRAND.toLowerCase().startsWith("huawei")) {
                if (TextUtils.isEmpty(Build.MANUFACTURER)) {
                    return v0;
                } else if (!Build.MANUFACTURER.toLowerCase().startsWith("huawei")) {
                    return v0;
                }
            }
        } catch (Throwable v1) {
            return v0;
        }

        return true;
    }

    private static NetworkType e(Context arg2) {
        NetworkType v0_2;
        try {
            NetworkInfo v0_1 = ((ConnectivityManager) arg2.getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();
            if (v0_1 == null || !v0_1.isAvailable()) {
                v0_2 = NetworkType.NONE;
                return v0_2;
            }

            int v0_3 = v0_1.getType();
            if (1 == v0_3) {
                return NetworkType.WIFI;
            }

            if (v0_3 != 0) {
                return NetworkType.MOBILE;
            }

            switch (((TelephonyManager) arg2.getSystemService(Context.TELEPHONY_SERVICE)).getNetworkType()) {
                case 13: {
                    return NetworkType.MOBILE_4G;
                }
                case 3:
                case 5:
                case 6:
                case 8:
                case 9:
                case 10:
                case 12:
                case 14:
                case 15: {
                    return NetworkType.MOBILE_3G;
                }
            }

            return NetworkType.MOBILE;


        } catch (Throwable v0) {
            v0_2 = NetworkType.MOBILE;
        }

        return v0_2;
    }
}

